﻿using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
using UselessExplorer.Controller;
using System.Drawing;

namespace UselessExplorer.UI
{
    public partial class ucFtp : UserControl
    {
        ClsGlobal cg = ClsGlobal.Instance;

        public ucFtp()
        {
            InitializeComponent();
        }

        private void ucFtp_Load(object sender, EventArgs e)
        {
            loadDirectories();
            localDirView.Drive = cbDriveList.SelectedItem.ToString();
        }

        private void loadDirectories()
        {
            string[] drives = Directory.GetLogicalDrives();

            foreach (string drive in drives)
            {
                cbDriveList.Items.Add(drive);
            }
            cbDriveList.SelectedItem = cbDriveList.Items[0];
        }

        private void loadFiles(string path, bool isRootNode)
        {
            try
            {
                this.localFileList.Items.Clear();

                string[] files = Directory.GetFiles(path);
                if (files.Length != 0)
                {
                    foreach (string file in files)
                    {

                        string fileName = isRootNode ? file.Substring(path.Length) : file.Substring(path.Length + 1);
                        localFileList.Items.Add(fileName);
                        localFileList.ContextMenuStrip = ftpUploadContext;
                    }
                }
                else
                {
                    localFileList.Items.Add("No Files Found");
                }
            }
            catch (Exception ex)
            {
                localDirView.SelectedNode.Remove();
                cg.WriteLogs(Logger.LogsCategory.Exception, ex);
            }
        }

        private void cbDriveList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string val = cbDriveList.SelectedItem.ToString();
            localDirView.Drive = val.Substring(0, 2);
            localDirView.RefreshDisplay();
        }

        



        private void clearDisplayedData()
        {
            ftpFileView.DataSource = null;
            ftpDirView.Nodes.Clear();
            lbStatusMsg.Items.Clear();
        }

        private void disableInputs()
        {
            tbFtpUsername.Enabled = false;
            tbFtpPassword.Enabled = false;
            tbFtpServer.Enabled = false;
            tbFtpPort.Enabled = false;
            btnFtpConnect.Enabled = false;
        }

        private void enableInputs()
        {
            tbFtpUsername.Enabled = true;
            tbFtpPassword.Enabled = true;
            tbFtpServer.Enabled = true;
            tbFtpPort.Enabled = true;
            btnFtpConnect.Enabled = true;
            btnFtpDisconnect.Enabled = false;
        }

        private void PrintStatusMessage(string msg)
        {
            lbStatusMsg.Items.Add(msg);
        }

        private void btnFtpConnect_Click(object sender, EventArgs e)
        {
            disableInputs();
            clearDisplayedData();

            FTPClient fc = new FTPClient(tbFtpServer.Text, tbFtpUsername.Text, tbFtpPassword.Text);
            string[] files = fc.GetFileList();
            TreeNode rootNode = new TreeNode(tbFtpServer.Text);
            ftpDirView.Nodes.Add(rootNode);

            if (files != null)
            {
                DataTable dt = new DataTable("Files");
                dt.Columns.Add(new DataColumn("Name"));
                dt.Columns.Add(new DataColumn("Size"));
                dt.Columns.Add(new DataColumn("Last Modified"));

                
                foreach (string file in files)
                {
                    float size = 0;
                    if (size != 0)
                    {
                        string timeStamp = fc.GetTimeStamp(file);
                        dt.Rows.Add(file, size + String.Empty + "MB", timeStamp);
                    }
                    else
                    {
                        TreeNode trN = new TreeNode(file);
                        rootNode.Nodes.Add(trN);
                    }

                }
                this.ftpDirView.Nodes[0].Expand();
                ftpFileView.DataSource = dt;
            }
            else
            {
                TreeNode noItems = new TreeNode("No Items Found");
                rootNode.Nodes.Add(noItems);
            }
        }



        



        private void localDirView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            localDirView.SelectedNode = e.Node;
            string path = e.Node.FullPath;
            bool isRootNode = false;
            if (localDirView.SelectedNode.Text == cbDriveList.SelectedItem.ToString())
                isRootNode = true;
            loadFiles(path, isRootNode);
        }

        private void btnFtpDisconnect_Click(object sender, EventArgs e)
        {
            enableInputs();
            clearDisplayedData();
        }

        private void tsUpload_Click(object sender, EventArgs e)
        {
            string fileToUpload = localFileList.SelectedItems[0].Text.ToString();
            string dirPath = localDirView.SelectedNode.FullPath;
            MessageBox.Show(localDirView.SelectedNode.FullPath.Replace(@"\\\", @"\") + @"\" + fileToUpload);
        }

        private void localFileList_SelectedIndexChanged(object sender, EventArgs e)
        {
            FileInfo fi = new FileInfo(localDirView.SelectedNode.FullPath + @"\" + localFileList.SelectedItems[0].Text.ToString());
            localFileList.DoDragDrop(fi, DragDropEffects.None);
        }

        private void localFileList_DragLeave(object sender, EventArgs e)
        {
            MessageBox.Show("Drag Out");
        }

        
    }
}
